package com.edu.study.base.country.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.edu.study.base.country.domain.Country;
import com.edu.study.base.country.service.ICountryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 国家Controller
 * 
 * @author zw
 * @date 2021-04-14
 */
@RestController
@RequestMapping("/country")
public class CountryController extends BaseController
{
    @Autowired
    private ICountryService countryService;

    /**
     * 查询国家列表
     */
    @PreAuthorize("@ss.hasPermi('country:list')")
    @GetMapping("/list")
    public TableDataInfo list(Country country)
    {
        startPage();
        List<Country> list = countryService.selectCountryList(country);
        return getDataTable(list);
    }

    /**
     * 导出国家列表
     */
    @PreAuthorize("@ss.hasPermi('country:export')")
    @Log(title = "国家", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Country country)
    {
        List<Country> list = countryService.selectCountryList(country);
        ExcelUtil<Country> util = new ExcelUtil<Country>(Country.class);
        return util.exportExcel(list, "country");
    }

    /**
     * 获取国家详细信息
     */
    @PreAuthorize("@ss.hasPermi('country:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(countryService.selectCountryById(id));
    }

    /**
     * 新增国家
     */
    @PreAuthorize("@ss.hasPermi('country:add')")
    @Log(title = "国家", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Country country)
    {
        country.setId(IdUtils.simpleUUID());
        country.setCreateDate(new Date());
        country.setCreateBy(SecurityUtils.getUsername());
        country.setDelFlag("0");
        return toAjax(countryService.insertCountry(country));
    }

    /**
     * 修改国家
     */
    @PreAuthorize("@ss.hasPermi('country:edit')")
    @Log(title = "国家", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Country country)
    {
        country.setUpdateDate(new Date());
        country.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(countryService.updateCountry(country));
    }

    /**
     * 删除国家
     */
    @PreAuthorize("@ss.hasPermi('country:remove')")
    @Log(title = "国家", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(countryService.deleteCountryByIds(ids));
    }
}
